package com.itheima.controller;

import com.itheima.domain.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    /**
     * url参数
     * localhost:8080/user/param1?name=zhangsan&age=18
     *
     * @param name
     * @param age
     * @return
     */
    @GetMapping("/param1")
    public String param1(String name, Integer age) {
        System.out.println("接收到参数name: " + name);
        System.out.println("接收到参数age: " + age);
        return "success";
    }

    /**
     * localhost:8080/user/param2?name=zhangsan&age=18
     *
     * @return
     */
    @GetMapping("/param2")
    public String param2(User user) {
        System.out.println("接收到参数user：" + user);
        return "success";
    }


    /**
     * url参数
     * localhost:8080/user/param3?username=zhangsan&age=18
     * 通过@RequestParam注解指定从请求中获取名为"username"的参数值，并将其类型转换为String，赋值给变量name。
     *
     * @param name
     * @param age
     * @return
     */
    @GetMapping("/param3")
    public String param3(@RequestParam("username") String name, Integer age) {
        System.out.println("接收到参数name: " + name);
        System.out.println("接收到参数age: " + age);
        return "success";
    }

    /**
     * http://localhost:8080/user/param4/101
     *
     * @return
     * @PathVariable作用：从路径中获取参数
     */
    @GetMapping("/param4/{id}")
    public String param4(@PathVariable("id") Integer id) {
        System.out.println("接收到参数 id:" + id);
        return "success";
    }

    /**
     * http://localhost:8080/user/param5?liked=swim&liked=game&liked=coding
     *
     * @return
     */
    @GetMapping("/param5")
    public String param5(@RequestParam List<String> liked) {
        System.out.println("接收到参数 liked:" + liked);
        return "success";
    }

    /**
     * POST http://localhost:8080/user
     * Body:
     * {
     * "name":"zhangsan",
     * "age":18
     * }
     * 将json字符串反序列化为对象
     *
     * @return
     */
    @PostMapping
    public String param6(@RequestBody User user) {
        System.out.println("接收到参数 user:" + user);
        return "success";
    }

    /**
     * GET http://localhost:8080/user/param7?birthday=2023年6月6日 18:18:18
     * 接收日期对象
     * @return
     */
    @GetMapping("/param7")
    public String param7(@DateTimeFormat(pattern = "yyyy年MM月dd日 HH:mm:ss") Date birthday) {
        System.out.println("接收到参数 birthday:" + birthday);
        return "success";
    }
}